package list;

public class SingleLinkedList {
    // 1、无头单向非循环链表实现

        public class LinkNode{
            public int val;
            LinkNode next;

            public LinkNode(int val) {
                this.val = val;
            }
        }
        LinkNode head = null;
     public LinkNode partition(LinkNode pHead, int x) {
         if(pHead == null){
             return pHead;
         }
        // write code here
         LinkNode sh = null;
         LinkNode st = null;
         LinkNode lh = null;
         LinkNode lt = null;
         while (pHead!=null){
             if(pHead.val<x){
                 if(sh == null){
                     st = sh =pHead;
                     pHead = pHead.next;
                 }else {
                     st.next = pHead;
                     st = pHead;
                     pHead = pHead.next;
                 }
             }else {
                 if(lh == null){
                     lt = lh =pHead;
                     pHead = pHead.next;
                 }else {
                     lt.next = pHead;
                     lt = pHead;
                     pHead = pHead.next;
                 }
             }
         }
         st.next = lh;
         return sh;

    }
        //头插法
        public void addFirst(int data){
            LinkNode linkNode = new LinkNode(data);
            if(head == null){
                head = linkNode;
                return;
            }
            linkNode.next = head;
            head = linkNode;
        };
        //尾插法
        public void addLast(int data){
            LinkNode cur = head;
            if(head == null){
                head = cur;
                return;
            }

            //先遍历到尾
            while (cur.next!=null){
                cur = cur.next;
            }
            LinkNode linkNode = new LinkNode(data);
            cur.next = linkNode;
        };
        //任意位置插入,第一个数据节点为0号下标
        public boolean addIndex(int index,int data){
            LinkNode prev = null;
            if (index<0){
                return false;
            }
            LinkNode cur = head;
            if(index == 0){
                addFirst(data);
                return true;
            }
            if(index >=size()){
                addLast(data);
                return true;
            }
            while (index>0){
                prev = cur;
                cur = cur.next;
                index--;
            }
            LinkNode linkNode = new LinkNode(data);
            linkNode.next = cur;
            prev.next = linkNode;
            return true;
        };
        //查找是否包含关键字key是否在单链表当中
        public boolean contains(int key){
            LinkNode cur = head;
            while (cur!=null){
                if(cur.val == key){
                    return true;
                }
                cur = cur.next;
            }
            return false;
        };
        //删除第一次出现关键字为key的节点
        public void remove(int key){
            LinkNode cur = head;
            LinkNode prev = null;
            if(head.val == key){
                head = head.next;
                return;
            }
            while (cur!=null){
               if(cur.val == key){
                   prev.next = cur.next;
               }
               prev = cur;
               cur = cur.next;
            }
        };
    private LinkNode findNodeOfKey(int key) {
        LinkNode cur = head;
        while (cur.next != null) {
            if(cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
        //删除所有值为key的节点
        public void removeAllKey(int key){
            if (head == null){
                return;
            }
            LinkNode prev = head;
            LinkNode cur = head.next;
            while (cur != null){
                if(cur.val == key){
                    prev.next = cur.next;
                    cur = cur.next;
                }else {
                    prev = cur;
                    cur = cur.next;
                }
            }
    }
        //得到单链表的长度
        public int size(){
            LinkNode cur = head;
            int length = 0;
            while (cur!=null){
                cur = cur.next;
                length++;
            }
            return length;
        }
        public void display(){
            LinkNode cur = head;
            while (cur!=null){
                System.out.print(cur.val+" ");
                cur = cur.next;
            }
            System.out.println();
        }
        public void clear(){
            head = null;
        };

}
